package cn.longge.controller;

import cn.longge.entity.AppVersion;
import cn.longge.entity.Result;
import cn.longge.service.IAppVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@RestController
@RequestMapping("manager/app")
public class AppVersionController {
    @Autowired
    private IAppVersion appVersionService;

    @RequestMapping("/index")
    public ModelAndView select(){
        return new ModelAndView("app/index");
    }

    @RequestMapping("/list/{page}/{pageSize}")
    public Result findAll(@PathVariable int page, @PathVariable int pageSize){
        Result result = new Result();
        result.setObj(appVersionService.selectByPage(page,pageSize));
        return result;
    }

    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate(){
        return new ModelAndView("app/update");
    }

    @PutMapping("/doUpdate")
    public Result doUpdate(@RequestBody AppVersion appVersion){
        appVersion.setUpdateDate(new Date());
        appVersionService.updateByPrimaryKeySelective(appVersion);
        Result result = new Result(true,"更新成功",null);
        return result;
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody AppVersion appVersion){
        appVersion.setCreateDate(new Date());
        appVersion.setUpdateDate(new Date());
        appVersion.setDelFlag("0");
        appVersion.setCreateBy("2,超级管理员");
        appVersionService.insertSelective(appVersion);
        Result result = new Result(true,"插入成功",null);
        return result;
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id){
        AppVersion appVersion = new AppVersion();
        appVersion.setId(Long.parseLong(id));
        appVersion.setUpdateDate(new Date());
        appVersion.setDelFlag("1");
        appVersionService.updateByPrimaryKeySelective(appVersion);
        Result result = new Result(true,"删除成功",null);
        return result;
    }
}
